#include "HiranipraShared.h"
#include "enet/enet.h"

#ifndef _SHARED_NET_ADDRESS_H_
#define _SHARED_NET_ADDRESS_H_

namespace Hiranipra {
	namespace Net {

		class Address
		{
		public:
			enum AddressConstants
			{
				Any = 0,
				LocalHost = 0x0100007f
			};

		public:
			Address( uint32 ip = Any, uint32 port = 0 ) :
				_hasResolved( false )
			{
				_address.host = ip;
				_address.port = port;
			}

			Address( string hostName, uint16 port )
			{
				LookupHostName( hostName );
				_address.port = port;
			}

			string GetHostName()
			{
				if( _hasResolved == true )
					return _hostName;
				else
				{
					char buffer[ 256 ];
					if( enet_address_get_host( &_address, buffer, 256 ) == 0 )
					{
						_hostName = string( buffer );
						_hasResolved = true;
						return _hostName;
					}
					else
						return "";
				}
			}

			bool LookupHostName( string hostName )
			{
				if( enet_address_set_host( &_address, hostName.c_str() ) == 0 )
				{
					_hostName = hostName;
					_hasResolved = true;
					return true;
				}
				else
					return false;
			}

			const ENetAddress* GetAddress() const { return &_address; }

		private:
			bool			_hasResolved;
			string			_hostName;

			ENetAddress		_address;
		};

	}
}

#endif // _SHARED_NET_ADDRESS_H_
